package net.wnb.mineclearance.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import net.wnb.mineclearance.model.Game;

public class SelectionPane extends JPanel implements ActionListener {
	public static final int BUTTON_W = 240;
	public static final int BUTTON_H = 40;
	private static final int DEFAULT_HEIGHT = 480;
	private static final int DEFAULT_WIDTH = 320;
	
	private JButton btn16;//16*16
	private JButton btn24;//16*24
	private JButton btn32;//16*32
	private JButton btnBack;//返回主菜单

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public SelectionPane(){
		init();
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		add(Box.createGlue());
		add(btn16);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(btn24);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(btn32);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(btnBack);
		add(Box.createGlue());
		
		setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		validate();
	}
	
	private void init(){
		btn16 = new JButton("简单(8*8)");
		btn24 = new JButton("中等(16*16)");
		btn32 = new JButton("困难(16*30)");
		
		btn16.setMaximumSize(new Dimension(BUTTON_W, BUTTON_H));
		btn24.setMaximumSize(new Dimension(BUTTON_W, BUTTON_H));
		btn32.setMaximumSize(new Dimension(BUTTON_W, BUTTON_H));
		
		btn16.setPreferredSize(new Dimension(BUTTON_W, BUTTON_H));
		btn24.setPreferredSize(new Dimension(BUTTON_W, BUTTON_H));
		btn32.setPreferredSize(new Dimension(BUTTON_W, BUTTON_H));
		
		btn16.setAlignmentX(CENTER_ALIGNMENT);
		btn24.setAlignmentX(CENTER_ALIGNMENT);
		btn32.setAlignmentX(CENTER_ALIGNMENT);
		
		btnBack = new JButton("返回菜单");
		btnBack.setMaximumSize(new Dimension(BUTTON_W, BUTTON_H));
		btnBack.setPreferredSize(new Dimension(BUTTON_W, BUTTON_H));
		btnBack.setAlignmentX(CENTER_ALIGNMENT);
		
		btn16.addActionListener(this);
		btn24.addActionListener(this);
		btn32.addActionListener(this);
		btnBack.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		Object source = event.getSource();
		if(source.equals(btn16)){
			Game game = new Game(8, 8, 10);
			loadGame(game);
		}else if(source.equals(btn24)){
			Game game = new Game(16, 16, 40);
			loadGame(game);
		}else if(source.equals(btn32)){
			Game game = new Game(16, 32, 80);
			loadGame(game);
		}else if(source.equals(btnBack)){
			MainWindow win = MainWindow.getInstance();
			MenuPane menuPane = new MenuPane();
			win.setPane(menuPane);
			win.validate();
		}
	}
	
	private void loadGame(Game game){
		GamePane pane = new GamePane(game);
		MainWindow win = MainWindow.getInstance();
		win.setPane(pane);
		game.start();
	}
}
